﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.Recharge
{
    /// <summary>
    /// 充值单列表
    /// </summary>
    public partial class RechargeBillListView : UserControl, IView
    {
        //当前页  
        private int _pageIndex = 0;
        //总共几页
        private int _pageCount = 0;
        /// <summary>
        /// 财务/收银进入标示
        /// </summary>
        private string flag = string.Empty;
        Promise _promise = new Promise();
        public RechargeBillListView()
        {
            InitializeComponent();
            this.GCData.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }
        public RechargeBillListView(string flag)
        {
            InitializeComponent();
            this.flag = flag;
            this.GCData.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }

        #region IView 成员
        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("view", Resources.isView, "查看", btnReview_Click))
                .AddAction(new ActionNode("recharge", Resources.isRecharge, "客户充值", btnRecharge_Click))
                .AddAction(new ActionNode("checked", Resources.isSettling, "收银", btnCheckOut_Click) { IsShow = false })
                 .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", btnRefresh_Click));
        }
        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs.Result && args.ClosedArgs.ActiveArgs != null)
            {
                if (args.ClosedArgs.View is Pickers.VCustomerAccuratelyPicker)
                {
                    WebRef.VCustomer item = args.ClosedArgs.Data as WebRef.VCustomer;
                    if (item != null)
                    {
                        this.ctlBtnSelectCustomer.Text = item.Name;

                        this.ctlBtnSelectCustomer.Tag = item;
                    }
                }
                else
                {
                    RefreshData();
                }
            }
        }
        public void Active(ViewActiveArgs args)
        {
            _promise.Then(() =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    SearchLookUpEditUtil.BindIntCodeTextItem(this.ctlSearchLookUpState,
                        Domain.Constant.CommonBillStatus.Enumerate(),
                        Domain.Constant.CommonBillStatus.AllItem, true);

                    if (!string.IsNullOrEmpty(flag))
                    {
                        this.lblState.Visible = false;
                        this.ctlSearchLookUpState.Visible = false;
                        this.ctlSearchLookUpState.EditValue = Domain.Constant.CommonBillStatus.Enabled;
                    }
                    _promise.Resolve();
                }));

            }).Then(RefreshData)
                .Run(null);
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("账户充值列表");
        }
        #endregion

        #region Method
        /// <summary>
        /// 获取每页条数限制
        /// </summary>
        /// <returns></returns>
        private int GetPageLimit()
        {
            int pagesize = 25;
            return pagesize;
        }
        /// <summary>
        /// 绑定数据列表
        /// </summary>
        private void RefreshData()
        {
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.VRechargeBill> result = new Service.QueryService().QueryRechargeBillPagedList(
                    GetSelectedCustomerId(),
                    ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId,
                    this.ctlSearchLookUpState.GetSelectedValue<Domain.IntCodeTextItem>().GetCode(Domain.Constant.CommonBillStatus.All),
                    _pageIndex * GetPageLimit(),
                    GetPageLimit());

                this.BeginInvoke(new Action<PagedResult<WebRef.VRechargeBill>>((p) =>
                {
                    if (p.IsSuccess)
                    {
                        this.GCData.DataSource = null;
                        this.GVData.FocusedRowHandle = -1;
                        this.GCData.Paginate<WebRef.VRechargeBill>(p, out _pageIndex, out _pageCount);
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(p.Message);
                        _promise.Reject();
                    }
                }), result);
            });
        }

        private Guid? GetSelectedCustomerId()
        {
            WebRef.VCustomer customer = ctlBtnSelectCustomer.Tag as WebRef.VCustomer;
            if (customer != null)
            {
                return customer.Id;
            }
            return null;
        }
        #endregion

        #region Action
        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReview_Click(object sender, object e)
        {

        }
        /// <summary>
        /// 充值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRecharge_Click(object sender, object e)
        {
            UIContext.Current.ViewManager.PopupView(new Create.CreateRechargeBillView(), new PopupViewArgs("客户充值单"));
        }
        /// <summary>
        /// 收银
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCheckOut_Click(object sender, object e)
        {
            if (this.GVData.FocusedRowHandle > -1)
            {
                WebRef.VRechargeBill item = this.GVData.GetFocusedRow() as WebRef.VRechargeBill;
                UIContext.Current.ViewManager.PopupView(new Recharge.CheckedOutRechargeBillView(item.Id), new PopupViewArgs("充值单收银"));
            }
        }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, object e)
        {
            _pageIndex = 0;
            RefreshData();
        }
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
            RefreshData();
        }
        /// <summary>
        /// 根据充值单状态显示对应的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GVData_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            WebRef.VRechargeBill item = this.GVData.GetFocusedRow() as WebRef.VRechargeBill;
            if (item == null) return;
            switch (item.Status)
            {
                case Domain.Constant.CommonBillStatus.Enabled:
                    this.ShowAction("checked", true);
                    break;
                default:
                    this.ShowAction("checked", false);
                    break;
            }
        }
        /// <summary>
        /// 选择客户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnSelectCustomer_Click(object sender, EventArgs e)
        {
            UIContext.Current.ViewManager.PopupView(new Pickers.VCustomerAccuratelyPicker(), new PopupViewArgs("选择购买客户"));
        }
        #endregion
    }
}
